package com.sfx.shortLink.mq.producer;


import cn.hutool.core.lang.UUID;
import com.sfx.shortLink.common.constants.RedisKeyConstant;
import com.sfx.shortLink.dto.ShortLinkStatsRecordDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
@RequiredArgsConstructor
public class ShortLinkStatsDelayQueueProducer {

    private final RedissonClient redissonClient;

    public void send(ShortLinkStatsRecordDTO statsRecordDTO) {
        statsRecordDTO.setKeys(UUID.fastUUID().toString());
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(RedisKeyConstant.DELAY_QUEUE_STATS_KEY);
        RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        delayedQueue.offer(statsRecordDTO, 5, TimeUnit.SECONDS);
    }
}